package app;

import com.utils.ByType;
import com.utils.LogCustom;
import com.utils.XmlReadUtil;
import com.utils.YamlReadUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;


public class BaseActionApp extends TestBaseCaseApp {

    protected HashMap<String, LocatorApp> locatorMap;
    public String path = null;
    public InputStream path_inputStream_1;
    public InputStream path_inputStream_2;
    LogCustom log = new LogCustom(this.getClass());

    public void setXmlObjectPath(String path) {
        this.path = path;
    }

    public void setXmlObjectPathForLocator(InputStream path_inputStream) {
        this.path_inputStream_1 = path_inputStream;
    }

    public void setXmlObjectPathForPageURL(InputStream path_inputStream) {
        this.path_inputStream_2 = path_inputStream;
    }

    /**
     * 构造方法，创建创建BasePageOpera对象时，需要初始化的信息.传递相关参数
     * this.getClass().getCanonicalName() 获取page类路径，也就是xml文档中的pageName
     *
     * @throws Exception
     */
    public BaseActionApp() {


    }

    public void getLocatorMap(String valueName) {
        XmlReadUtil xmlReadUtil = new XmlReadUtil();
        YamlReadUtil yamlReadUtil = new YamlReadUtil();
        try {
            if ((path == null || path.isEmpty())) {
                locatorMap = xmlReadUtil.readXMLDocumentApp(path_inputStream_1, valueName);
            } else {
                if (path.contains(".xml")) {
                    locatorMap = xmlReadUtil.readXMLDocumentApp(path, valueName);
                } else if (path.contains(".yaml")) {
                    locatorMap = yamlReadUtil.getLocatorMap(path, valueName);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从对象库获取定位信息
     *
     * @param locatorName 对象库名字
     * @return
     * @throws IOException
     */
    public LocatorApp getLocator(String locatorName) {
        LocatorApp locatorApp;
        /**
         * 在对象库通过对象名字查找定位信息
         */
        locatorApp = locatorMap.get(locatorName);
        /**
         * 加入对象库，找不到该定位信息，就创建一个定位信息
         */
        if (locatorApp == null) {
            log.error("没有找到" + locatorName + "页面元素");
        }
        return locatorApp;

    }

    public String getPageURL() {
        String pageURL = null;
        try {
            if (path == null || path.isEmpty()) {
                pageURL = XmlReadUtil.getXmlPageURL(path_inputStream_1, this.getClass().getCanonicalName());
            } else {
                pageURL = XmlReadUtil.getXmlPageURL(path, this.getClass().getCanonicalName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageURL;
    }

    /**
     * 打开浏览器
     *
     * @param url
     */
    public void open(String url) {
        driver.navigate().to(url);
        log.info("打开浏览器，访问" + url + "网址!");

    }

    /***
     * 关闭浏览器窗口
     */
    public void close() {
        driver.close();
        log.info("关闭浏览器窗口");
    }

    /**
     * 退出浏览器
     */
    public void quit() {
        driver.quit();
        log.info("退出浏览器");
    }

    /**
     * 浏览器前进
     */
    public void forward() {
        driver.navigate().forward();
        log.info("浏览器前进");
    }

    /**
     * 浏览器后退
     */
    public void back() {
        driver.navigate().back();
        log.info("浏览器后退");
    }

    /**
     * 刷新浏览器
     */
    public void refresh() {
        driver.navigate().refresh();
        log.info("浏览器刷新");
    }

    public WebElement findElement(final LocatorApp locatorApp) throws IOException {
        /**
         * 查找某个元素等待几秒
         */
        Waitformax(Integer.valueOf(locatorApp.getWaitSec()));
        WebElement webElement;
        webElement = getElement(locatorApp);
        return webElement;


    }
    public void Waitformax(int t) {
        driver.manage().timeouts().implicitlyWait(t, TimeUnit.SECONDS);
    }

    /**
     * 通过定位信息获取元素
     *
     * @param locatorApp
     * @return
     * @throws NoSuchElementException
     */
    public WebElement getElement(LocatorApp locatorApp) {
        /**
         * locatorApp.getElement(),获取对象库对象定位信息
         */
        //locatorApp=getLocator(locatorMap.get(key));
        WebElement webElement;
        switch (locatorApp.getType()) {
            case xpath:
                //log.info("find element By xpath");
                webElement = driver.findElement(By.xpath(locatorApp.getElement()));
                /**
                 * 出现找不到元素的时候，记录日志文件
                 */
                break;
            case id:
                //log.info("find element By xpath");
                webElement = driver.findElement(By.id(locatorApp.getElement()));
                break;
            case cssSelector:
                //log.info("find element By cssSelector");
                webElement = driver.findElement(By.cssSelector(locatorApp.getElement()));
                break;
            case name:
                //log.info("find element By name");
                webElement = driver.findElement(By.name(locatorApp.getElement()));
                break;
            case className:
                //log.info("find element By className");
                webElement = driver.findElement(By.className(locatorApp.getElement()));
                break;
            case linkText:
                //log.info("find element By linkText");
                webElement = driver.findElement(By.linkText(locatorApp.getElement()));
                break;
            case partialLinkText:
                //log.info("find element By partialLinkText");
                webElement = driver.findElement(By.partialLinkText(locatorApp.getElement()));
                break;
            case tagName:
                //log.info("find element By tagName");
                webElement = driver.findElement(By.partialLinkText(locatorApp.getElement()));
                break;
            default:
                //log.info("find element By xpath");
                webElement = driver.findElement(By.xpath(locatorApp.getElement()));
                break;

        }
        return webElement;
    }


}
